﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using PPAP.Model;
using System.Data.OracleClient;

namespace PPAP.DAO
{
    public class TestDepartmentDAO
    {
        public TestDepartmentDAO() { }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Testdepartment model)
        {
            model.ID = DbHelperOra.GetMaxID("ID", "TESTDEPARTMENT");
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into TESTDEPARTMENT(");
            strSql.Append("ID,TESTITEMNAME,DEPARTMENTID,REMARK,CREATETIME,CREATEBY)");
            strSql.Append(" values (");
            strSql.Append(":ID,:TESTITEMNAME,:DEPARTMENTID,:REMARK,:CREATETIME,:CREATEBY)");
            OracleParameter[] parameters = {
					new OracleParameter(":ID", OracleType.Number,4),
					new OracleParameter(":TESTITEMNAME", OracleType.VarChar,50),
					new OracleParameter(":DEPARTMENTID", OracleType.Number,4),
					new OracleParameter(":REMARK", OracleType.VarChar,4000),
                                           new OracleParameter(":CREATETIME",OracleType.DateTime),
                                           new OracleParameter(":CREATEBY",OracleType.VarChar,50)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.TESTITEMNAME;
            parameters[2].Value = model.DEPARTMENTID;
            parameters[3].Value = model.REMARK;
            parameters[4].Value = model.CreateTime;
            parameters[5].Value = model.Createby;

            return DbHelperOra.ExecuteSql(strSql.ToString(), parameters)>0?int.Parse(model.ID.ToString()):0;
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int Update(Testdepartment model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update TESTDEPARTMENT set ");
            strSql.Append("ID=:ID,");
            strSql.Append("TESTITEMNAME=:TESTITEMNAME,");
            strSql.Append("DEPARTMENTID=:DEPARTMENTID,");
            strSql.Append("REMARK=:REMARK,");
            strSql.Append("CREATETIME=:CREATETIME,");
            strSql.Append("CREATEBY=:CREATEBY");
            strSql.Append(" where id="+model.ID);
            OracleParameter[] parameters = {
					new OracleParameter(":ID", OracleType.Number,4),
					new OracleParameter(":TESTITEMNAME", OracleType.VarChar,50),
					new OracleParameter(":DEPARTMENTID", OracleType.Number,4),
					new OracleParameter(":REMARK", OracleType.VarChar,4000),
                                           new OracleParameter(":CREATETIME",OracleType.DateTime),
                                           new OracleParameter(":CREATEBY",OracleType.VarChar,50)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.TESTITEMNAME;
            parameters[2].Value = model.DEPARTMENTID;
            parameters[3].Value = model.REMARK;
            parameters[4].Value = model.CreateTime;
            parameters[5].Value = model.Createby;

           return DbHelperOra.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public int Delete(int id)
        {
            //该表无主键信息，请自定义主键/条件字段
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from TESTDEPARTMENT ");
            strSql.Append(" where id="+id);
            OracleParameter[] parameters = {
};

           return DbHelperOra.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Testdepartment GetModel(int id)
        {
            //该表无主键信息，请自定义主键/条件字段
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,TESTITEMNAME,DEPARTMENTID,REMARK,CREATETIME,createby from TESTDEPARTMENT ");
            strSql.Append(" where id="+id);
            OracleParameter[] parameters = {
};

            Testdepartment model = new Testdepartment();
            DataSet ds = DbHelperOra.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                model.TESTITEMNAME = ds.Tables[0].Rows[0]["TESTITEMNAME"].ToString();
                if (ds.Tables[0].Rows[0]["DEPARTMENTID"].ToString() != "")
                {
                    model.DEPARTMENTID = int.Parse(ds.Tables[0].Rows[0]["DEPARTMENTID"].ToString());
                }
                model.REMARK = ds.Tables[0].Rows[0]["REMARK"].ToString();
                model.CreateTime = DateTime.Parse(ds.Tables[0].Rows[0]["CREATETIME"].ToString());
                model.Createby = ds.Tables[0].Rows[0]["createby"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select s.ID,TESTITEMNAME,DEPARTMENTID,REMARK,s.CREATETIME,s.createby,rownum,d.department from TESTDEPARTMENT s left join DEPARTMENT d on s.departmentid=d.id ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperOra.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取测试部门人员
        /// </summary>
        /// <returns></returns>
        public DataSet GetUserForTest()
        {
            string sql = "select * from users where departmentid=6";
            return DbHelperOra.Query(sql);
        }

        /// <summary>
        /// 获取测试部门人员
        /// </summary>
        /// <returns></returns>
        public DataSet GetUserForTest(string testdepartment)
        {
            string sql = "select t.username from TESTDEPARTMENTCARD t left join users u on t.username=u.username where u.isexecuteionspectiontask=1 and t.testdepartment=" + testdepartment;
            sql += " order by t.username ";
            return DbHelperOra.Query(sql);
        }
    }
}
